home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / if2sixel / part01
Encoding:
Internet Message Format  |  1990-06-03  |  24.0 KB

  1. Path: xanth!cs.odu.edu!Amiga-Request
  2. From: Amiga-Request@cs.odu.edu (Amiga Sources/Binaries Moderator)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v90i172: iff2sixel 1.21 - convert IFF ILBM to SIXEL , Part01/01
  5. Message-ID: <12697@xanth.cs.odu.edu>
  6. Date: 3 Jun 90 22:08:25 GMT
  7. Sender: tadguy@cs.odu.edu
  8. Reply-To: Tony Kennedy <KENNEDY%WWPACS%dupont.com@RELAY.CS.NET>
  9. Lines: 884
  10. Approved: tadguy@cs.odu.edu (Tad Guy)
  11. X-Mail-Submissions-To: Amiga@cs.odu.edu
  12. X-Post-Discussions-To: comp.sys.amiga
  13.  
  14. Submitted-by: Tony Kennedy <KENNEDY%WWPACS%dupont.com@RELAY.CS.NET>
  15. Posting-number: Volume 90, Issue 172
  16. Archive-name: util/iff2sixel-1.21/part01
  17.  
  18. This program takes a AMIGA IFF ILBM file and converts it to a SIXEL file
  19. to be displayed on DIGITAL EQUIPMENT CORP. VT240, VT241, VT340
  20. terminals.
  21.               
  22. This version will handle only 16 colors for a VT340 and 4 colors for a
  23. VT240. It does no color compression, so all input files must be 16
  24. colors or less for a 340, 4 colors or less for a 240. Or, up to 64
  25. colors for a printer.
  26.  
  27. If the display is a VT240/241 then the resolution is set to 640X200. If
  28. it's a 340 then the res is 640X400.  PRINT gives a straight pixel for
  29. pixel representation.
  30.  
  31. I tried to make it a generic as possible so you can compile it under
  32. what ever environment you please.  If you do it under VMS you will need
  33. to define IFF2SIXEL as a foreign command i.e.  $ iff2sixel :==
  34. $mydir:iff2sixel otherwise you can't use command line args.
  35.  
  36. #!/bin/sh
  37. # This is a shell archive.  Remove anything before this line, then unpack
  38. # it by saving it into a file and typing "sh file".  To overwrite existing
  39. # files, type "sh file -c".  You can also feed this as standard input via
  40. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  41. # will see the following message at the end:
  42. #        "End of archive 1 (of 1)."
  43. # Contents:  iff2sixel.c
  44. # Wrapped by tadguy@xanth on Sun Jun  3 18:08:11 1990
  45. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  46. if test -f 'iff2sixel.c' -a "${1}" != "-c" ; then 
  47.   echo shar: Will not clobber existing file \"'iff2sixel.c'\"
  48. else
  49. echo shar: Extracting \"'iff2sixel.c'\" \(20888 characters\)
  50. sed "s/^X//" >'iff2sixel.c' <<'END_OF_FILE'
  51. X/*****************************************************************************/
  52. X/*                                         
  53. X    Program  : IFF2SIXEL.C
  54. X
  55. X    Author   : Tony Kennedy
  56. X           Internet address: kennedam%wwps@dupont.com@relay.cs.net
  57. X
  58. X    Purpose  : This program takes a AMIGA IFF ILBM file and converts
  59. X           it to a SIXEL file to be displayed on DIGITAL EQUIPMENT
  60. X           CORP. VT240, VT241, VT340 terminals. 
  61. X                                 
  62. X           This version will handle only 16 colors for a VT340 and
  63. X           4 colors for a VT240. It does no color compression, so
  64. X           all input files must be 16 colors or less for a 340,
  65. X           4 colors or less for a 240. Or, up to 64 colors for a
  66. X           printer.
  67. X
  68. X           If the display is a VT240/241 then the resolution
  69. X           is set to 640X200. If it's a 340 then the res is 640X400.
  70. X           PRINT gives a straight pixel for pixel representation.
  71. X
  72. X           I tried to make it a generic as possible so you can 
  73. X           compile it under what ever environment you please.
  74. X           If you do it under VMS you will need to define IFF2SIXEL
  75. X           as a foreign command i.e.  $ iff2sixel :== $mydir:iff2sixel
  76. X           otherwise you can't use command line args.
  77. X
  78. X    Usage    : iff2sixel <input_file> <output_file> <VT240 | VT340 | PRINT>
  79. X
  80. X    Comments : This program is Freely Distributable. You may
  81. X           use it for any non-commercial uses.  
  82. X                   (If anybody hacks at this please send me a copy.)
  83. X
  84. X    Acknowledgements : Thanks to...
  85. X
  86. X                 Carter Brock for the SHOW_IFF program I used
  87. X                to figure out how to read an IFF file.
  88. X
  89. X                Mark Thompson and Steve Berry for the IFF2SUN
  90. X                program I used to figure out how to interpret
  91. X                the bitplanes.
  92. X
  93. X                Barry Young for his help with SIXEL formats.
  94. X
  95. X                Daniel Jay Barrett for his switching bytes
  96. X                hacks.
  97. X
  98. X                All the people at Dupont who tested it on 
  99. X                their pictures. (Hi Mike!)
  100. X
  101. X    Limitations : This won't do HAM. When I figure it out
  102. X              I'll put that in too.
  103. X              Won't do any color compression.
  104. X              I had some trouble getting this to run on the Amiga.
  105. X              It needs a REALLY big stack.  Next time I rewrite it,
  106. X              I'll do something about that.
  107. X
  108. X    Versions:        Revisions : 
  109. X    
  110. X    1.0  3/1/89         Original program.
  111. X    1.1  5/5/89        Modified to read Deluxe Paint III files.
  112. X                More robust IFF header read routines.
  113. X    1.2  7/14/89        Changed Argument line routine.
  114. X                Changed SixelLine Array to do one line at
  115. X                a time instead of the whole thing at once.
  116. X                Added #define H_LINES, V_LINES, N_COLORS
  117. X                Changed all unsigned short to short.
  118. X    1.21 4/4/90        Added Halfbrite, overscan, 7 bit control codes.
  119. X                Checks for PAL. Printer codes for LJ250 ink jet
  120. X                printer.
  121. X*/
  122. X/*****************************************************************************/
  123. X
  124. X#include <stdio.h>
  125. X#include <math.h>
  126. X
  127. X#define Sixel_base 63
  128. X#define DCS        144
  129. X#define ST         156
  130. X#define QUOTE      34
  131. X
  132. X#define LoRes  1
  133. X#define LoLace 2
  134. X#define HiRes  3
  135. X#define HiLace 4
  136. X
  137. X#define X320x200 10
  138. X#define Y320x200 11
  139. X#define X320x400 20
  140. X#define Y320x400 11
  141. X#define X640x200 5
  142. X#define Y640x200 11
  143. X#define X640x400 10
  144. X#define Y640x400 11
  145. X
  146. X#define VT240_LoRes  1
  147. X#define VT240_LoLace 2
  148. X#define VT240_HiRes  3
  149. X#define VT240_HiLace 4
  150. X#define VT340_LoRes  5
  151. X#define VT340_LoLace 6
  152. X#define VT340_HiRes  7
  153. X#define VT340_HiLace 8
  154. X
  155. X#define H_LINES 1500
  156. X#define V_LINES 600
  157. X#define N_COLORS 64
  158. X
  159. X#define V_LACE 400
  160. X#define V_NOLACE 200
  161. X#define V_LACE_PAL 512
  162. X#define V_NOLACE_PAL 256 
  163. X#define H_HIRES 640
  164. X#define H_LORES 320
  165. X
  166. X#define SUCCESS 1
  167. X
  168. X typedef unsigned char ID_type[4];
  169. X struct ChunkType
  170. X    {
  171. X        ID_type Ident ;
  172. X        long    ChunkSize ;
  173. X    } ;
  174. X struct ChunkType chunk ;
  175. X
  176. X typedef struct 
  177. X    {
  178. X        unsigned char red, green, blue;
  179. X    } ColorRegType;                   
  180. X
  181. X unsigned char BitPlaneArray[480][6][128];   /* vert pos, plane, width pos*/  
  182. X unsigned char PixelArray[480][H_LINES];     /* vert posit, horiz posit   */
  183. X char           SixelLine[N_COLORS][H_LINES]; /* color,horizontal posit.   */
  184. X int           vert, horz, s_vert, v_bit;
  185. X
  186. X/********************************************************************
  187. X
  188. X    This function decompresses the IFF file. I got the algorithm
  189. X    from Mark and Steve who got it from Leo (The Great Caped One).
  190. X
  191. X********************************************************************/
  192. X
  193. X int decompress_planes(nplanes,width,height,inp_file)
  194. X
  195. X short nplanes,width,height;
  196. X FILE *inp_file;
  197. X {
  198. X   long h,p,count,BytesPerRow;
  199. X   char len;
  200. X   unsigned char byte;
  201. X
  202. X   for (h=0;h<height;h++)
  203. X     {
  204. X    for (p=0;p<nplanes;p++)
  205. X      {
  206. X         count = 0;
  207. X         BytesPerRow=width/8;
  208. X         while(BytesPerRow>0)
  209. X        {
  210. X          if ((len = getc(inp_file)) >= 0)
  211. X             {
  212. X            BytesPerRow -= ++len;
  213. X            fread(&BitPlaneArray[h][p][count],len,1,inp_file);
  214. X            count += len;
  215. X             }
  216. X          else 
  217. X             if (len < 0)
  218. X             {
  219. X            len = -len + 1;
  220. X            BytesPerRow -= len;
  221. X            byte = getc(inp_file);
  222. X            while (--len >= 0)
  223. X                BitPlaneArray[h][p][count++] = byte;
  224. X             }
  225. X        }
  226. X         if (BytesPerRow) printf("Compression is corrupt.\n");
  227. X    }
  228. X     }
  229. X  }
  230. X
  231. X/********************************************************************
  232. X
  233. X    Since the VAX reads shorts and longs backwards we've got to 
  234. X    flip them around...
  235. X
  236. X********************************************************************/
  237. X
  238. X#ifdef VAX
  239. X
  240. Xshort flip_byte_short(s)
  241. X
  242. X  short s;
  243. X  {
  244. X    short hib,lob;
  245. X
  246. X    lob = (s & 255);
  247. X    hib = ((s >> 8) & 255);
  248. X    return ((lob << 8) + hib);
  249. X  }
  250. X
  251. Xlong flip_bytes_long(l)
  252. X
  253. X  long l;
  254. X  {
  255. X    short low,hiw;
  256. X
  257. X    low = (l & 65535);
  258. X    hiw = ((l >> 16) & 65535);
  259. X    return ( ((flip_byte_short(low) << 16) + flip_byte_short(hiw)));
  260. X  }
  261. X#endif
  262. X
  263. X/********************************************************************
  264. X
  265. X    Convert a string to upper case.
  266. X
  267. X********************************************************************/
  268. X
  269. Xchar *upcase(str)
  270. X  char *str;
  271. X
  272. X  {
  273. X    int len, i;
  274. X
  275. X    i=0;
  276. X    len=strlen(str);
  277. X    while(i != len)
  278. X        {
  279. X          str[i]=toupper(str[i]);
  280. X          i++;
  281. X        }
  282. X    return str;
  283. X  }
  284. X
  285. X/********************************************************************
  286. X
  287. X    Double the width of the Pixel Array.
  288. X
  289. X********************************************************************/
  290. X
  291. X short double_width(width,height)
  292. X
  293. X  short width,height;
  294. X
  295. X  {
  296. X    short h,w;
  297. X    for (h=0;h<height;h++)
  298. X      {
  299. X        for (w=width-1;w>=0;w--)
  300. X        {
  301. X          PixelArray[h][w * 2] = PixelArray[h][w];
  302. X          PixelArray[h][(w * 2)+1] = PixelArray[h][w];
  303. X        }                       
  304. X      }
  305. X      return (width * 2);
  306. X  }
  307. X
  308. X/********************************************************************
  309. X
  310. X    Double the height of the Pixel array.
  311. X
  312. X********************************************************************/
  313. X
  314. X short double_height(width,height)
  315. X
  316. X  short width,height;
  317. X
  318. X  {
  319. X    short h,w;
  320. X    for (w=0;w<width;w++)
  321. X      for (h=height-1;h>=0;h--)
  322. X        {
  323. X          PixelArray[h * 2][w]=PixelArray[h][w];
  324. X          PixelArray[(h * 2)+1][w]=PixelArray[h][w];
  325. X        }
  326. X    return (height * 2);
  327. X  }
  328. X
  329. X/********************************************************************
  330. X
  331. X    Halve the height of the Pixel Array. This is gonna make the
  332. X    picture look pretty ugly if you ask me. Only gets done if a
  333. X    picture for a VT240 from an interlace IFF file is being 
  334. X    generated.
  335. X
  336. X********************************************************************/
  337. X
  338. Xshort halve_height(width,height)
  339. X
  340. X  short width,height;
  341. X
  342. X  {
  343. X    short h,w;
  344. X    for (h=0;h<height;h++)
  345. X        for (w=0;w<=width;w++)
  346. X            PixelArray[h][w]=PixelArray[(h * 2)][w];
  347. X
  348. X    return (height/2);
  349. X  }                                            
  350. Xmain (argc,argv)
  351. X
  352. X int argc;
  353. X char **argv[];
  354. X {
  355. X  long num_colors;
  356. X  int stat;
  357. X  long posit,h,w,p,i,j,k,l,m,pixel,v_pos;
  358. X  long w_pos,cnt_100,cnt_10,cnt_1,rep_cnt;
  359. X  char ch, outline[900];
  360. X  unsigned char  color;
  361. X  char *term;
  362. X  FILE *outf,*inf;
  363. X  char start_sixel = DCS;
  364. X  char end_sixel = ST;
  365. X  char quote = QUOTE;
  366. X  int  convert_mode = 0;
  367. X  int  resolution;
  368. X
  369. X  struct BitMapHeaderType
  370. X    {
  371. X        short width, height;
  372. X        short          org_x, org_y;
  373. X        unsigned char  NumPlanes, Mask, Compression, pad;
  374. X        unsigned short TransColor;
  375. X        unsigned char  X_Asp, Y_Asp;
  376. X        unsigned short PageWid, PageHeight;
  377. X    } ;
  378. X  struct BitMapHeaderType bmhd;
  379. X                              
  380. X  ColorRegType CMap[N_COLORS];
  381. X  
  382. X/*********************************************************************
  383. X
  384. X First, get all the needed info from the user...            
  385. X              
  386. X    Input file, Output file, Type of terminal to create a file for.
  387. X
  388. X*********************************************************************/
  389. X                                 
  390. X  switch (argc)
  391. X    {                                                   
  392. X    case 4:
  393. X        
  394. X        inf = fopen(argv[1],"r");
  395. X
  396. X            if (inf == NULL) {
  397. X                  printf("Input file can't be opened.\n");
  398. X                  exit(0); }
  399. X                                                                          
  400. X        outf = fopen(argv[2],"w");
  401. X
  402. X            if (outf == NULL) {
  403. X                  printf("Output can't be opened.\n");
  404. X            close(inf);
  405. X                  exit(0); }        
  406. X
  407. X        term = argv[3];
  408. X                                                             
  409. X                  if  ((strcmp(upcase(term),"VT240",5) != 0) &&
  410. X                       (strcmp(upcase(term),"VT340",5) != 0) &&
  411. X             (strcmp(upcase(term),"PRINT",5) != 0))
  412. X             {
  413. X            printf("Not a valid terminal type. VT240 or VT340.\n");
  414. X            close(inf);close(outf);
  415. X            exit(0);
  416. X             }                                  
  417. X/*        resolution = atoi(argv[4]);
  418. X        if ((resolution<1)||(resolution>4)) {
  419. X          printf("1 - LoRes\n 2 - LoLace\n3 - HiRes\n4 - HiLace\n");
  420. X          close(inf);close(outf);
  421. X          exit(0);}
  422. X*/
  423. X        break;
  424. X
  425. X    default:                     
  426. X                                                        
  427. X        printf("\nIFF2Sixel Conversion Program.\n");
  428. X        printf("  V1.21 4/90 Tony Kennedy\n");    
  429. X      printf("\nUSAGE : IFF2Sixel <input> <output> <VT240|VT340|PRINT>\n");
  430. X      exit(0);             
  431. X                                                        
  432. X    }
  433. X                        
  434. X                                                                      
  435. X
  436. X/*********************************************************************
  437. X
  438. X    Ok. Now read in the IFF file...
  439. X
  440. X*********************************************************************/
  441. X
  442. X    
  443. X    stat=fread(chunk.Ident,4,1,inf);
  444. X    if (stat == -1) 
  445. X        {
  446. X            printf("Error reading file.\n");
  447. X            stat=close(inf) ; stat=close(outf);
  448. X            exit(0);
  449. X                }
  450. X                                     
  451. X
  452. X    while ((strncmp(chunk.Ident,"FORM",4) !=0) && (stat==SUCCESS))
  453. X
  454. X        {
  455. X        stat=fread(&ch,1,1,inf);
  456. X        for (i=0; i < 3; i++)
  457. X            chunk.Ident[i] = chunk.Ident[i+1];
  458. X        chunk.Ident[3]=ch;
  459. X        }                                  
  460. X                                 
  461. X    if (strncmp(chunk.Ident,"FORM",4) != 0) 
  462. X        {
  463. X            printf("Error, not a FORM file.\n");
  464. X            stat=close(inf); stat=close(outf);
  465. X            exit(0);
  466. X        }
  467. X
  468. X    stat=fread(&chunk.ChunkSize,4,1,inf);
  469. X
  470. X    stat=fread(chunk.Ident,4,1,inf);
  471. X                                               
  472. X    while ((strncmp(chunk.Ident,"ILBM",4) !=0) && (stat==SUCCESS)) 
  473. X
  474. X             
  475. X        {
  476. X        stat=fread(&ch,1,1,inf);
  477. X        for (i=0; i < 3; i++)
  478. X            chunk.Ident[i] = chunk.Ident[i+1];
  479. X        chunk.Ident[3]=ch;
  480. X        }                                                      
  481. X    if (strncmp(chunk.Ident, "ILBM",4) !=0) 
  482. X                                               
  483. X        {
  484. X            printf("Error Not an ILBM file.\n");
  485. X                  stat=close(inf); stat=close(outf);
  486. X            exit(0);
  487. X        }                              
  488. X    stat=fread(chunk.Ident,4,1,inf);
  489. X        
  490. X    while ((strncmp(chunk.Ident,"BMHD",4) !=0) && (stat==SUCCESS))
  491. X
  492. X             
  493. X        {
  494. X        stat=fread(&ch,1,1,inf);
  495. X        for (i=0; i < 3; i++)
  496. X            chunk.Ident[i] = chunk.Ident[i+1];
  497. X        chunk.Ident[3]=ch;
  498. X        }                                                      
  499. X    if (strncmp(chunk.Ident, "BMHD",4) !=0)
  500. X        {
  501. X            printf("Error can't find Bit Map Header.\n");
  502. X                  stat=close(inf); stat=close(outf);
  503. X            exit(0);
  504. X
  505. X        }                              
  506. X     stat=fread(&chunk.ChunkSize,4,1,inf);
  507. X
  508. X    stat=fread(&bmhd,20,1,inf);
  509. X             
  510. X    if (stat == -1)
  511. X        {
  512. X            printf("Error failed to get Bit Map information.\n");
  513. X                  stat=close(inf); stat=close(outf);
  514. X            exit(0);
  515. X        }
  516. X
  517. X
  518. X    stat=fread(chunk.Ident,4,1,inf);
  519. X        
  520. X    while ((strncmp(chunk.Ident,"CMAP",4) !=0) && (stat==SUCCESS))
  521. X
  522. X             
  523. X        {
  524. X        stat=fread(&ch,1,1,inf);
  525. X        for (i=0; i < 3; i++)
  526. X            chunk.Ident[i] = chunk.Ident[i+1];
  527. X        chunk.Ident[3]=ch;
  528. X        }                                                      
  529. X    if (strncmp(chunk.Ident,"CMAP",4) !=0)
  530. X        {
  531. X            printf("Error can't find Color Map.\n");
  532. X                  stat=close(inf); stat=close(outf);
  533. X            exit(0);
  534. X        }                 
  535. X        stat=fread(&num_colors,4,1,inf);           
  536. X
  537. X#ifdef VAX
  538. X
  539. X    bmhd.width=flip_byte_short(bmhd.width);
  540. X    bmhd.height=flip_byte_short(bmhd.height);
  541. X    bmhd.org_x=flip_byte_short(bmhd.org_x);
  542. X    bmhd.org_y=flip_byte_short(bmhd.org_y);
  543. X    bmhd.TransColor=flip_byte_short(bmhd.TransColor);
  544. X    bmhd.PageWid=flip_byte_short(bmhd.PageWid);
  545. X    bmhd.PageHeight=flip_byte_short(bmhd.PageHeight);
  546. X    num_colors=flip_bytes_long(num_colors);
  547. X
  548. X#endif
  549. X
  550. X/********************************************************************
  551. X
  552. X    define conversion mode.  This is really convoluted because 
  553. X    there doesn't seem to be a consistant way of telling what 
  554. X    resolution a pictures is in.  This checks Page Width and 
  555. X    Height, and XY aspect ratio.
  556. X
  557. X********************************************************************/
  558. X                                  
  559. X
  560. X    if ((bmhd.PageWid==H_LORES)&&
  561. X        ((bmhd.PageHeight==V_NOLACE)||(bmhd.PageHeight==V_NOLACE_PAL))
  562. X       ) 
  563. X        resolution=LoRes;
  564. X    if ((bmhd.PageWid==H_LORES)&&
  565. X         ((bmhd.PageHeight==V_LACE)||(bmhd.PageHeight==V_LACE_PAL))
  566. X        )                        
  567. X        resolution=LoLace;
  568. X    if ((bmhd.PageWid==H_HIRES)&&
  569. X        ((bmhd.PageHeight==V_NOLACE)||(bmhd.PageHeight==V_NOLACE_PAL))
  570. X       )
  571. X        resolution=HiRes;
  572. X    if ((bmhd.PageWid>=H_HIRES)&&
  573. X         ((bmhd.PageHeight>=V_LACE)||(bmhd.PageHeight>=V_LACE_PAL))
  574. X        )                   
  575. X        resolution=HiLace;                        
  576. X
  577. X
  578. X    if ((bmhd.X_Asp==X320x200)&&
  579. X        (bmhd.Y_Asp==Y320x200))     resolution=LoRes;
  580. X
  581. X    if ((bmhd.X_Asp==X320x400)&&
  582. X        (bmhd.Y_Asp==Y320x400))     resolution=LoLace;
  583. X
  584. X    if ((bmhd.X_Asp==X640x200)&&
  585. X        (bmhd.Y_Asp==Y640x200))     resolution=HiRes;
  586. X
  587. X    if ((bmhd.X_Asp==X640x400)&&
  588. X        (bmhd.Y_Asp==Y640x400)&&
  589. X        (bmhd.PageHeight>=V_LACE))    resolution=HiLace;
  590. X
  591. X
  592. X    if  ((strcmp(upcase(term),"VT240",5) == 0) &&  
  593. X         (resolution==LoRes)) convert_mode=VT240_LoRes;
  594. X
  595. X    if  ((strcmp(upcase(term),"VT240",5) == 0) &&  
  596. X         (resolution==LoLace)) convert_mode=VT240_LoLace;
  597. X
  598. X    if  ((strcmp(upcase(term),"VT240",5) == 0) &&  
  599. X         (resolution==HiRes)) convert_mode=VT240_HiRes;
  600. X
  601. X    if  ((strcmp(upcase(term),"VT240",5) == 0) &&  
  602. X         (resolution==HiLace)) convert_mode=VT240_HiLace;
  603. X
  604. X    if  ((strcmp(upcase(term),"VT340",5) == 0) &&  
  605. X         (resolution==LoRes)) convert_mode=VT340_LoRes;
  606. X
  607. X    if  ((strcmp(upcase(term),"VT340",5) == 0) &&  
  608. X         (resolution==LoLace)) convert_mode=VT340_LoLace;
  609. X
  610. X    if  ((strcmp(upcase(term),"VT340",5) == 0) &&  
  611. X         (resolution==HiRes)) convert_mode=VT340_HiRes;
  612. X
  613. X    if  ((strcmp(upcase(term),"VT340",5) == 0) &&  
  614. X         (resolution==HiLace)) convert_mode=VT340_HiLace;
  615. X
  616. X        if  (strcmp(upcase(term),"PRINT",5) == 0)
  617. X                  convert_mode=VT340_HiLace;
  618. X        
  619. X    
  620. X
  621. X    if (!convert_mode) 
  622. X        {
  623. X        printf("IFF picture is an irregular size.\n");
  624. X        printf("Converting pixel to pixel representation.\n");
  625. X        convert_mode=VT340_HiLace;
  626. X           }
  627. X
  628. X
  629. X    num_colors=num_colors/3;                         
  630. X    stat=fread(CMap,(num_colors * 3),1,inf);
  631. X                
  632. X        if ( (num_colors > N_COLORS) ||
  633. X         ((num_colors > 4) && (strcmp(upcase(term),"VT240",5) == 0))
  634. X       )
  635. X
  636. X        {
  637. X           printf("File has too many colors for this conversion.\n");
  638. X           close(inf);
  639. X           close(outf);
  640. X           exit(0);
  641. X        }
  642. X    if (bmhd.NumPlanes == 6) {
  643. X        for (i=0;i<32;i++)
  644. X          {
  645. X            CMap[i+32].red = CMap[i].red>>1;
  646. X            CMap[i+32].blue = CMap[i].blue>>1;
  647. X            CMap[i+32].green = CMap[i].green>>1;
  648. X          }
  649. X        num_colors = 64;
  650. X                }
  651. X
  652. X
  653. X/********************************************************************
  654. X
  655. X    Skip everything till we find the BODY.
  656. X
  657. X********************************************************************/
  658. X                                         
  659. X    stat=fread(chunk.Ident,4,1,inf); 
  660. X    while (strncmp(chunk.Ident,"BODY",4) !=0)  
  661. X        {
  662. X        stat=fread(&ch,1,1,inf);
  663. X        for (i=0; i < 3; i++)
  664. X            chunk.Ident[i] = chunk.Ident[i+1];
  665. X        chunk.Ident[3]=ch;
  666. X        }                                  
  667. X                                 
  668. X
  669. X    stat=fread(chunk.Ident,4,1,inf); /* body length */
  670. X
  671. X
  672. X/********************************************************************
  673. X
  674. X    Now read the bit planes in.
  675. X
  676. X********************************************************************/
  677. X
  678. X    if (bmhd.Compression == 1)
  679. X        decompress_planes(bmhd.NumPlanes,bmhd.width,bmhd.height,inf);
  680. X    else
  681. X
  682. X        for (h = 0;h<bmhd.height;h++)
  683. X            for (p = 0;p<bmhd.NumPlanes;p++) 
  684. X                {
  685. X                 for (w = 0;w<bmhd.width/8;w++) 
  686. X                    {
  687. X                     if (feof(inf)) continue;
  688. X                     read(inf,BitPlaneArray[h][p][w],1);
  689. X                            }
  690. X                                }
  691. X    close(inf);
  692. X
  693. X/********************************************************************
  694. X
  695. X    Translate bit planes to pixel color values.
  696. X
  697. X********************************************************************/
  698. X
  699. X        for (h = 0;h<bmhd.height;h++)
  700. X     {
  701. X     w_pos = 0;
  702. X     for (w=0;w<bmhd.width/8;w++)
  703. X       {                                                   
  704. X        pixel = 0;    
  705. X        for (i=7;i>=0;i--)
  706. X          {
  707. X           
  708. X        for (p=0;p<bmhd.NumPlanes;p++)
  709. X          pixel = ((((1 << i) & BitPlaneArray[h][p][w])>>i)<<p)|pixel;
  710. X           
  711. X        PixelArray[h][w_pos++] = (unsigned char)(pixel); 
  712. X        pixel = 0;
  713. X          }                                              
  714. X       }
  715. X     }    
  716. X
  717. X/********************************************************************
  718. X
  719. X    Convert to screen size.
  720. X
  721. X********************************************************************/
  722. X
  723. X    switch(convert_mode)
  724. X
  725. X        {
  726. X             case VT240_LoRes : 
  727. X              bmhd.width = double_width(bmhd.width,bmhd.height);
  728. X                          break;                        
  729. X
  730. X                 case VT240_LoLace :
  731. X              bmhd.height = halve_height(bmhd.width,bmhd.height);
  732. X              bmhd.width = double_width(bmhd.width,bmhd.height);
  733. X              break;
  734. X             
  735. X             case VT240_HiRes : 
  736. X              break;
  737. X                      
  738. X             case VT240_HiLace :
  739. X              bmhd.height = halve_height(bmhd.width,bmhd.height);
  740. X              break;
  741. X
  742. X             case VT340_LoRes :
  743. X              bmhd.width = double_width(bmhd.width,bmhd.height);
  744. X              bmhd.height = double_height(bmhd.width,bmhd.height);
  745. X              break;
  746. X
  747. X             case VT340_LoLace :
  748. X              bmhd.width = double_width(bmhd.width,bmhd.height);
  749. X                          break;                        
  750. X
  751. X             case VT340_HiRes :
  752. X              bmhd.height = double_height(bmhd.width,bmhd.height);
  753. X              break;
  754. X
  755. X             case VT340_HiLace :
  756. X              break;
  757. X
  758. X        }
  759. X                
  760. X/********************************************************************
  761. X
  762. X    Convert Pixels to Sixels.
  763. X
  764. X********************************************************************/
  765. X
  766. X    vert=bmhd.height;
  767. X    horz=bmhd.width;
  768. X    v_bit = 0;
  769. X                            
  770. X                                                   
  771. X/********************************************************************
  772. X
  773. X    Compress Sixels and write to file.
  774. X
  775. X********************************************************************/
  776. X
  777. X    
  778. X    vert = bmhd.height/6;
  779. X
  780. X/*    Write Sixel start code. */
  781. X
  782. X    fprintf(outf,"%cP;1;8;q%c1;1;;\n",27,quote);
  783. X
  784. X/*    Write color map. */
  785. X
  786. X    for (color=0;color<num_colors;color++)
  787. X      {
  788. X        CMap[color].red   = (CMap[color].red * 100)/255;        
  789. X        CMap[color].green = (CMap[color].green * 100)/255;
  790. X        CMap[color].blue  = (CMap[color].blue * 100)/255;
  791. X        fprintf(outf,"#%d;2;%d;%d;%d\n",color,
  792. X                CMap[color].red,CMap[color].green,CMap[color].blue);
  793. X
  794. X      }
  795. X      fprintf(outf,"\n");
  796. X
  797. X
  798. X/*     Compress and write Sixels. */
  799. X
  800. X    v_pos = 0;
  801. X
  802. X    for (s_vert=0;s_vert <= vert;s_vert++)
  803. X     {
  804. X        
  805. X    for (i=0;i<N_COLORS;i++)
  806. X        {
  807. X           for (j=0;j<horz;j++)
  808. X            SixelLine[i][j] = 0;
  809. X        }
  810. X    
  811. X    for (i=0;i<6;i++)
  812. X       {        
  813. X        for (j=0;j<horz && v_pos<bmhd.height;j++)
  814. X           {
  815. X            color = PixelArray[v_pos][j];                               
  816. X            SixelLine[color][j] |= (int)pow(2,i);
  817. X           }      
  818. X        v_pos++;
  819. X       }
  820. X                    
  821. X
  822. X       for (color=0;color<num_colors;color++)
  823. X        {
  824. X
  825. X          for (j=0;j<horz;j++)                                
  826. X            SixelLine[color][j] += Sixel_base;
  827. X          i = 0;
  828. X          k = 0;
  829. X          while( i < horz )
  830. X        {
  831. X           ch = SixelLine[color][i];            
  832. X           l=i;
  833. X
  834. X           while ((l < horz) && (ch == SixelLine[color][l]))
  835. X            l++;
  836. X           rep_cnt = l - i;
  837. X           if (rep_cnt >= 5)
  838. X            {
  839. X               cnt_100 = rep_cnt/100;
  840. X               cnt_10  = (rep_cnt-(cnt_100 * 100))/10;
  841. X               cnt_1   = (rep_cnt-((cnt_100 * 100)+(cnt_10 * 10)));
  842. X               outline[k] = 33; /* Exclamation mark */
  843. X               outline[++k] = cnt_100 + 48;
  844. X               outline[++k] = cnt_10  + 48;
  845. X               outline[++k] = cnt_1   + 48;
  846. X               outline[++k] = SixelLine[color][l-1];
  847. X               k++;
  848. X               i=l;
  849. X            }
  850. X           else
  851. X            {
  852. X               for (m=0;m<=(l-1)-i;m++)
  853. X                   outline[k+m]=SixelLine[color][l-1];
  854. X               k += (l-i);
  855. X               i=l;
  856. X            }
  857. X        }         
  858. X        outline[k]=0;
  859. X        if (strcmp(outline,"!640?",5))
  860. X        fprintf(outf,"#%d %s$\n",color,&outline); 
  861. X
  862. X        }
  863. X        fprintf(outf," - \n");
  864. X    }
  865. X    fprintf(outf,"%c\\\n",27);
  866. X    close(outf);
  867. X     }
  868. END_OF_FILE
  869. if test 20888 -ne `wc -c <'iff2sixel.c'`; then
  870.     echo shar: \"'iff2sixel.c'\" unpacked with wrong size!
  871. fi
  872. # end of 'iff2sixel.c'
  873. fi
  874. echo shar: End of archive 1 \(of 1\).
  875. cp /dev/null ark1isdone
  876. MISSING=""
  877. for I in 1 ; do
  878.     if test ! -f ark${I}isdone ; then
  879.     MISSING="${MISSING} ${I}"
  880.     fi
  881. done
  882. if test "${MISSING}" = "" ; then
  883.     echo You have the archive.
  884.     rm -f ark[1-9]isdone
  885. else
  886.     echo You still need to unpack the following archives:
  887.     echo "        " ${MISSING}
  888. fi
  889. ##  End of shell archive.
  890. exit 0
  891. -- 
  892. Mail submissions (sources or binaries) to <amiga@cs.odu.edu>.
  893. Mail comments to the moderator at <amiga-request@cs.odu.edu>.
  894. Post requests for sources, and general discussion to comp.sys.amiga.
  895.